package com.szxw.BaiduAi.controller;

import com.alibaba.fastjson2.JSON;
import com.baidu.aip.nlp.ESimnetType;
import com.szxw.BaiduAi.service.NlpService;
import com.szxw.BaiduAi.pojo.Emotions;
import com.szxw.BaiduAi.pojo.Items;
import com.szxw.BaiduAi.service.EmotionService;
import com.szxw.BaiduAi.service.impl.EmoServiceIml;
import com.szxw.BaiduAi.utils.EsimnetType;
import com.szxw.BaiduAi.utils.MethodUtils;
import org.json.JSONObject;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author: 苏执小威
 * @Email:1410511702@qq.com
 * @Desc: 自然语言模块
 * @Date: 2022/9/19 22:13
 */
@WebServlet("/nlp")
public class NlpController extends BaiDuController {

    private EmotionService service = new EmoServiceIml();

    /**
     * 该方法用于实现 情感分析
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    public void emotion(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取参数
        String text = req.getParameter("text");
        Object emotion = service.emotion(text);
        MethodUtils.close(resp, emotion);
    }

    /**
     * 该方法用于实现 地址识别
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    public void address(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取参数
        String addr = req.getParameter("text");
        //调用接口进行地址识别并得到结果
        JSONObject address = NlpService.addr(addr);
        //将结果响应到前端
        MethodUtils.close(resp, address);

    }

    /**
     * 该方法用于实现 文本纠错
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    public void correction(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取参数
        String addr = req.getParameter("text");
        //调用接口进行地址识别并得到结果
        JSONObject address = NlpService.correction(addr);
        //将结果响应到前端
        MethodUtils.close(resp, address);
    }

    /**
     * 该方法用于实现 新闻摘要
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    public void newsSummary(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取参数
        String text = req.getParameter("text");
        int len = Integer.parseInt(req.getParameter("chang"));
        JSONObject newSum = NlpService.newsSummary(text, len);
        MethodUtils.close(resp, newSum);
    }

    /**
     * 该方法用于实现 文本相似度
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    public void textSimolarity(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取参数
        String text1 = req.getParameter("text1");
        String text2 = req.getParameter("text2");
        JSONObject textSim = NlpService.textSim(text1, text2);
        MethodUtils.close(resp, textSim);
    }

    /**
     * 该方法用于实现 词义相似度
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    public void wordSiment(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取参数
        String word1 = req.getParameter("word1");
        String word2 = req.getParameter("word2");
        JSONObject wordSim = NlpService.textSim(word1, word2);
        MethodUtils.close(resp, wordSim);
    }

    /**
     * 该方法用于实现 词法分析
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    public void lexAnalysis(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取参数
        String text = req.getParameter("text");
        JSONObject lexer = NlpService.lexer(text);
        MethodUtils.close(resp, lexer);
    }

    /**
     * 该方法用于实现 依存句法分析
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    public void wordParsing(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取参数
        String text = req.getParameter("text");
        JSONObject parser = NlpService.depParser(text);
        MethodUtils.close(resp, parser);
    }

    /**
     * 该方法用于实现 文章标签
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    public void artLabel(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取参数
        String title = req.getParameter("title");
        String text = req.getParameter("text");
        JSONObject parser = NlpService.keyWord(title, text);
        MethodUtils.close(resp, parser);
    }

    /**
     * 该方法用于实现 文章分类
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    public void artCate(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取参数
        String title = req.getParameter("title");
        String text = req.getParameter("text");
        JSONObject parser = NlpService.topic(title, text);
        MethodUtils.close(resp, parser);
    }

    /**
     * 该方法用于实现 情感对话分析
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    public void dialogue(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取参数
        String text = req.getParameter("text");
        JSONObject tolk = NlpService.emotolk(text);
        MethodUtils.close(resp, tolk);
    }

    /**
     * 该方法用于实现 评论观点抽取
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    public void opComments(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取参数
        String text = req.getParameter("text");
        String type = req.getParameter("type");
        ESimnetType type1 = EsimnetType.getESimnetType(type);
        JSONObject parser = NlpService.comTag(text, type1);
        MethodUtils.close(resp, parser);
    }

    /**
     * 该方法用于实现 词向量表示
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    public void wordVector(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取参数
        String text = req.getParameter("text");
        JSONObject word = NlpService.wordEmbed(text);
        MethodUtils.close(resp, word);
    }

    /**
     * 该方法用于实现 DNN语言模型
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    public void dnnModel(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取参数
        String text = req.getParameter("text");
        JSONObject parser = NlpService.dnnlmcn(text);
        MethodUtils.close(resp, parser);
    }


}
